package com.fduss.artStore.dao.impl;

import com.fduss.artStore.dao.ArtistDao;
import com.fduss.artStore.entity.Artist;
import com.fduss.artStore.util.JdbcUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by alex on 29/06/2017.
 */
public class ArtistDaoImpl implements ArtistDao {

    private JdbcUtil util;

    public ArtistDaoImpl() {
        util = new JdbcUtil();
    }

    @Override
    public Artist add(Artist artist) {
        Connection conn = util.getConnection();
        String sql = "INSERT INTO `art`.`artist` " +
                "(`ArtistID`, `FirstName`, `LastName`, `Nationality`, `YearOfBirth`, `YearOfDeath`, `Details`, `ArtistLink`)" +
                " VALUES (?, ?, ?, ?, ?, ?, ?, ?)";

        PreparedStatement pst = null;
        boolean flag = false;

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, artist.getArtistId());
            pst.setString(2, artist.getFirstName());
            pst.setString(3, artist.getLastName());
            pst.setString(4, artist.getNationality());
            pst.setInt(5, artist.getYearOfBirth());
            pst.setInt(6, artist.getYearOfDeath());
            pst.setString(7, artist.getDetails());
            pst.setString(8, artist.getArtistLink());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, pst, conn);
        }

        return flag ? artist : null;
    }

    @Override
    public void delete(int artistID) {
        Connection conn = util.getConnection();
        String sql = "DELETE FROM `art`.`artist` WHERE `ArtistID` = ?";
        PreparedStatement pst = null;

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, artistID);
            pst.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, pst, conn);
        }


    }

    @Override
    public Artist update(Artist artist) {
        Connection conn = util.getConnection();
        String sql = "UPDATE `art`.`artist` SET `FirstName`=?, `LastName`=?, `Nationality`=?, `YearOfBirth`=?, `YearOfDeath`=?, `Details`=?, `ArtistLink`=? WHERE `ArtistID`= ?";
        PreparedStatement pst = null;
        boolean flag = false;

        try {
            pst = conn.prepareStatement(sql);
            pst.setString(1, artist.getFirstName());
            pst.setString(2, artist.getLastName());
            pst.setString(3, artist.getNationality());
            pst.setInt(4, artist.getYearOfBirth());
            pst.setInt(5, artist.getYearOfDeath());
            pst.setString(6, artist.getDetails());
            pst.setString(7, artist.getArtistLink());
            pst.setInt(8, artist.getArtistId());
            pst.executeUpdate();
            flag = true;
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(null, pst, conn);
        }
        return flag ? artist : null;
    }

    @Override
    public Artist getOne(int artistID) {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`artist` WHERE `ArtistID` = ?";
        PreparedStatement pst = null;
        ResultSet rs = null;
        Artist artist = null;

        try {
            pst = conn.prepareStatement(sql);
            pst.setInt(1, artistID);
            rs = pst.executeQuery();

            if (rs.next()) {
                artist = buildArtist(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            util.close(rs, pst, conn);
        }

        return artist;
    }

    @Override
    public List<Artist> getAll() {
        Connection conn = util.getConnection();
        String sql = "SELECT * FROM `art`.`artist`";
        PreparedStatement pst = null;
        ResultSet rs = null;
        List<Artist> artist = new ArrayList<>();

        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();

            while (rs.next()){
                artist.add(buildArtist(rs));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            util.close(rs, pst, conn);
        }
        return artist;
    }

    private Artist buildArtist(ResultSet rs) throws SQLException {
        Artist artist = new Artist();
        artist.setArtistId(rs.getInt("ArtistID"));
        artist.setArtistLink(rs.getString("ArtistLink"));
        artist.setDetails(rs.getString("Details"));
        artist.setFirstName(rs.getString("FirstName"));
        artist.setLastName(rs.getString("LastName"));
        artist.setNationality(rs.getString("Nationality"));
        artist.setYearOfBirth(rs.getInt("YearOfBirth"));
        artist.setYearOfDeath(rs.getInt("YearOfDeath"));
        return artist;
    }
}
